/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.policy;

import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.requests.CreateRequest;

import com.cea.papyrus.core.editpart.LayoutEditPolicy;
import com.cea.papyrus.diagram.sequence.sedi.edit.GuardEditPart;
import com.cea.papyrus.diagram.sequence.sedi.edit.handlers.GuardLayoutHandler;

/**
 * Layout policy that is installed on Combined Fragments edit parts.
 *
 * @author Andreas Limyr
 * @author frankd@ifi.uio.no
 */
public class CFLayoutPolicy extends LayoutEditPolicy {

    public CFLayoutPolicy() {
		super(new XYLayout());
	}
    
	protected Command createAddCommand(EditPart child, Object constraint) {
		return null;
	}

	@Override
	protected Command createChangeConstraintCommand(
			final EditPart childEP, final Object constraint) {
		
		CompoundCommand ccmd = new CompoundCommand();
		
		Command[] cmds = null;
		if (childEP instanceof GuardEditPart) {
			cmds = new GuardLayoutHandler().getMoveCommands(getHost(), childEP, (Rectangle) constraint);
			ccmd.setLabel("Move Guard");
		}

		if (cmds != null) {
			for (Command c : cmds)
				ccmd.add(c);
			return ccmd;
		}
		return null;
	}

	@Override
	protected Command getCreateCommand(final CreateRequest request) {
		return null;
	}

	// The LayoutEditPolicy implementation always installs a PRIMARY_DRAG_ROLE
	// edit policy on children, overriding any that is already installed.
	// We actually really want to keep the one we have installed on the editpart,
	// because resizing and movement policies differs for some editparts.
	// EOs, for example, can only resize downwards, and several editparts
	// snap to other objects in different ways.
	@Override
	protected void decorateChild(EditPart child) {
		if (child.getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE) == null)
			super.decorateChild(child);
	}

	/*
	protected EditPolicy createChildEditPolicy(EditPart child) {
		return new ResizableEditPolicy();
	} */
	
	@Override
	protected Command getDeleteDependantCommand(Request arg0) {
		return null;
	}
}
